package com.org.gameloft.GLIDER.plugins.server;

import com.org.gameloft.GLIDER.common.JarResources;

import java.util.Hashtable;
import java.util.Properties;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Jul 31, 2007
 * Time: 5:39:15 PM
 * Revision: 0
 */
public class PluginLoader extends ClassLoader
{
    private Hashtable classes = new Hashtable();

    private JarResources jarResources;

    public PluginLoader(String jarName)
    {
        jarResources = new JarResources(jarName);
    }

    public Class loadClass(String className) throws ClassNotFoundException
    {
        return (loadClass(className, true));
    }

    public synchronized Class loadClass(String className, boolean resolveIt)
            throws ClassNotFoundException
    {

        Class result;
        byte[] classBytes;

        result = (Class) classes.get(className);
        if (result != null)
        {
            return result;
        }

        try
        {
            result = super.findSystemClass(className);
            return result;
        }
        catch (ClassNotFoundException e)
        {
            //not a system class, continue loading
        }

        classBytes = jarResources.getResource(className.replace('.','/')+".class");
        if (classBytes == null)
        {
            throw new ClassNotFoundException();
        }

        result = defineClass(className, classBytes, 0, classBytes.length);
        if (result == null)
        {
            throw new ClassFormatError();
        }

        if (resolveIt)
            resolveClass(result);

        classes.put(className, result);
        return result;
    }
}
